





ALTER           procedure  sp_insert_address_for_existing_salesorders

as
begin
	declare @salesOrder_Id int
	declare @salesOrder_no varchar(50)
        declare @phoneNumber varchar(50)
        declare @faxNumber varchar(50)
	declare @email varchar(50)
	declare @billing_address_id int

        delete from Sales_Order_Address
        
	declare insert_address_orders CURSOR For
	SELECT  Salesorder_Id, Salesorder_No ,So_Billing_Address_Id FROM Sales_Order 
	
	open insert_address_orders		
	fetch next from  insert_address_orders into  @salesOrder_Id , @salesOrder_no  , @billing_address_id
	
	while @@fetch_status = 0
	begin
		
		select @phoneNumber = Phone_Number , @faxNumber = Fax_Number , @email = Email FROM Address  WHERE Address_Id = @billing_address_id 
		insert into Sales_Order_Address (SalesOrder_Id , Phone_Number , Fax_Number ,Email )values (@salesOrder_Id , @phoneNumber , @faxNumber , @email )
		fetch next from insert_address_orders into @salesOrder_Id , @salesOrder_no  , @billing_address_id
		
	end 

	close insert_address_orders
	deallocate insert_address_orders
       
	select * from Sales_Order_Address
 

END

